package com.ruoyi.charge.utils;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 充电桩Verticle管理器，用于跟踪和管理所有部署的充电桩Verticle实例
 * @author laifeng
 * @version 1.0
 * @date 2025/7/13 12:55
 */
public class ClientManager {

    // 存储充电桩SN与Verticle部署ID的映射关系
    private final Map<String, String> deploymentMap = new ConcurrentHashMap<>();
    private static final ClientManager INSTANCE = new ClientManager();

    private ClientManager() {
        // 私有构造函数，防止外部实例化
    }

    public static ClientManager getInstance() {
        return INSTANCE;
    }

    /**
     * 添加Verticle部署记录
     * @param chargerSn 充电桩序列号
     * @param deploymentId Verticle部署ID
     */
    public synchronized void addClient(String chargerSn, String deploymentId) {
        deploymentMap.put(chargerSn, deploymentId);
    }

    /**
     * 获取充电桩对应的Verticle部署ID
     * @param chargerSn 充电桩序列号
     * @return 部署ID，不存在则返回null
     */
    public String getDeploymentId(String chargerSn) {
        return deploymentMap.get(chargerSn);
    }

    /**
     * 删除充电桩Verticle部署记录
     * @param chargerSn 充电桩序列号
     */
    public synchronized void removeClient(String chargerSn) {
        deploymentMap.remove(chargerSn);
    }

    /**
     * 获取所有已部署的充电桩SN
     */
    public Set<String> getAllChargerSns() {
        return deploymentMap.keySet();
    }

    /**
     * 检查充电桩是否已部署
     * @param chargerSn 充电桩序列号
     */
    public boolean isDeployed(String chargerSn) {
        return deploymentMap.containsKey(chargerSn);
    }
}